package library_system.Util;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author haomin
 * @date 2022/04/05 19:24
 **/
public class DBUtil {
    private static final DataSource dataSource;

    static{
        MysqlDataSource db = new MysqlDataSource();
        // 端口+地址
        db.setServerName("localhost");
        db.setPort(3306);
        // 认证
        db.setUser("root");
        db.setPassword("123456");
        // 设置默认库
        db.setDatabaseName("0331_library");
        // 其他配置
        db.setUseSSL(false);
        db.setCharacterEncoding("utf-8");
        db.setServerTimezone("Asia/Shanghai");

        dataSource = db;
    }

    public static Connection connection () throws SQLException {
        return dataSource.getConnection();

    }
}